﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhDataAcess.orderLinqDAL;
using dhSystemBLL.orderData;
using dhSystemBLL.basicData;
using dhsystem.produceForm;
using baseSet.importExport;
using dhDataAcess.baseBLL;
using baseSet;


namespace dhsystem.orderForm
{
    public partial class FrmorderDetailAdd : Form
    {
        orderDetailBLL orderDetail = new orderDetailBLL();
        orderDetailModel orderDetailRow = new orderDetailModel();
        productListBLL productList = new productListBLL();
        orderMasterBLL orderMaster = new orderMasterBLL();
        productUnitBLL productunit = new productUnitBLL();
        colorNumListBLL colorNum = new colorNumListBLL();
        productTypeBLL productType = new productTypeBLL();
        basListBLL basList = new basListBLL();
        BindingSource BindingSourceorderDetailAdd;
        string state;
        string orderNo;
        string orderDetailNo = "P" + GlobalInfo.Date + basicSql.getOrderSerail("漂染单号");
        //绑定数据源
        public FrmorderDetailAdd(BindingSource bindingSourceorderDetail, string _state, string _orderNo)
        {
            InitializeComponent();
            BindingSourceorderDetailAdd = bindingSourceorderDetail;
            state = _state;
            orderNo = _orderNo;
        }

        //打开窗口时绑定数据
        private void FrmorderDetailAdd_Load(object sender, EventArgs e)
        {
           
            bindingSourceunit.DataSource = productunit.getproductUnitAll ();
            comboBoxunit.DataSource = bindingSourceunit;
            comboBoxunit.DisplayMember = "unitEn";
            comboBoxunit.ValueMember = "unitEn";
            bindingSourcecolorNum.DataSource = colorNum.getcolorNumListAll();
            //comboBoxcolorNum.DataSource = bindingSourcecolorNum;
            //comboBoxcolorNum.DisplayMember = "colorNum";
            //comboBoxcolorNum.ValueMember = "colorNum";
            //bindingSourcebasList.DataSource = basList.getbasListByType("生产方式");
            //comboxprotype.DataSource = bindingSourcebasList;
            //comboxprotype.DisplayMember = "basNameI";
            //comboxprotype.ValueMember = "basNameI";
            //comboxprotype.SelectedValue = "自厂";
           

            if (state == "编辑")
            {
                textorderNo.DataBindings.Add("text", BindingSourceorderDetailAdd, "orderNo");
                textorderDetailNo.DataBindings.Add("text", BindingSourceorderDetailAdd, "orderDetailNo");
                textpname.DataBindings.Add("text", BindingSourceorderDetailAdd, "pname");
                textpcolor.DataBindings.Add("text", BindingSourceorderDetailAdd, "pcolor");
                textcolorNum.DataBindings.Add("text", BindingSourceorderDetailAdd, "colorNum");
                comboBoxunit.DataBindings.Add("SelectedValue", BindingSourceorderDetailAdd, "unit");
                textqty.DataBindings.Add("text", BindingSourceorderDetailAdd, "qty");
                textprice.DataBindings.Add("text", BindingSourceorderDetailAdd, "price");
                textaccount.DataBindings.Add("text", BindingSourceorderDetailAdd, "account");
                comboxprotype.DataBindings.Add("text", BindingSourceorderDetailAdd, "proType");
                textmark.DataBindings.Add("text", BindingSourceorderDetailAdd, "mark");
                textptype.DataBindings.Add("text", BindingSourceorderDetailAdd, "ptype");
                textorderNo.Text = orderNo;
               
               
                textqty.ReadOnly = true;
                
              
            }
            if (state == "新增")
            {

               
                textorderNo.DataBindings.Add("text", BindingSourceorderDetailAdd, "orderNo");
                textorderDetailNo.DataBindings.Add("text", BindingSourceorderDetailAdd, "orderDetailNo");
                textpname.DataBindings.Add("text", BindingSourceorderDetailAdd, "pname");
                textpcolor.DataBindings.Add("text", BindingSourceorderDetailAdd, "pcolor");
                textcolorNum.DataBindings.Add("text", BindingSourceorderDetailAdd, "colorNum");
                comboBoxunit.DataBindings.Add("SelectedValue", BindingSourceorderDetailAdd, "unit");
                textqty.DataBindings.Add("text", BindingSourceorderDetailAdd, "qty");
                textprice.DataBindings.Add("text", BindingSourceorderDetailAdd, "price");
                textaccount.DataBindings.Add("text", BindingSourceorderDetailAdd, "account");
                comboxprotype.DataBindings.Add("text", BindingSourceorderDetailAdd, "proType");
                textmark.DataBindings.Add("text", BindingSourceorderDetailAdd, "mark");
                textptype.DataBindings.Add("text", BindingSourceorderDetailAdd, "ptype");
                textorderNo.Text = orderNo;
                comboBoxunit.Text = "Kg";
                comboxprotype.Text = "自产";
                textptype.Text = "梳织";
                
               // textorderDetailNo.Text = "P" + GlobalInfo.Date + basicSql.getOrderSerail("漂染单号");
              //  BindingSourceorderDetailAdd.AddNew();
                textorderNo.Text = orderNo;
               
            }

           // textorderDetailNo.Focus();
                
        }
        //保存按钮，插入或修改数据
        private void buttonOK_Click(object sender, EventArgs e)
        {
            try
            {
                //DialogResult result = MessageBox.Show("是否保存资料！", "提示信息", MessageBoxButtons.YesNo);
                //if (checkBox1.Checked == true)
                //{
                //    result = DialogResult.No;
                //}
                //if (result == DialogResult.Yes)
                //{
                string orderSerial = basicSql.getOrderSerail("漂染单号");
                basList.basListstretbasNumUpdate((Convert.ToInt32(orderSerial) + 1).ToString(), "漂染单号");

                    BindingSourceorderDetailAdd.EndEdit(); //编辑到完成，写到数据源
                    BindingSourceorderDetailAdd.CancelEdit(); //刷新数据作用
                    BindingSourceorderDetailAdd.AddNew();
                //}
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        //取消按钮
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            BindingSourceorderDetailAdd.CancelEdit();
            this.Close();

        }

        private void buttuserselect_Click(object sender, EventArgs e)
        {
            string field;
            field = "pname";
            Frmprodutselect productselect = new Frmprodutselect(moduleInfo.productList , field);
            DialogResult result = productselect.ShowDialog();
            if (result == DialogResult.Yes)
            {
                textpname.Text  = productselect.pname;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string field;
            field = "colorNuM";
            Frmprodutselect productselect = new Frmprodutselect(moduleInfo.colorNumList , field);
            DialogResult result = productselect.ShowDialog();
            if (result == DialogResult.Yes)
            {
                textcolorNum.Text  = productselect.pname;
            }
        }

        private void textqty_TextChanged(object sender, EventArgs e)
        {
            if (textqty.Text != "" && textprice.Text  != "")
            {
                textaccount.Text = Convert.ToString(Convert.ToDecimal(textprice.Text) * Convert.ToDecimal(textqty.Text));
            }
        }

        private void textprice_TextChanged(object sender, EventArgs e)
        {
            if (textqty.Text != "" && textprice.Text != "")
            {
                textaccount.Text = Convert.ToString(Convert.ToDecimal(textprice.Text) * Convert.ToDecimal(textqty.Text));
            }
        }

       

        private void textpname_TextChanged(object sender, EventArgs e)
        {
            textptype .Text  = basicSql.getptypebyproductList(textpname.Text);
        }

        private void textorderNo_TextChanged(object sender, EventArgs e)
        {
            textorderDetailNo.Text = orderDetailNo;
        }
    }
}
